<?php

class ChapterController extends AdminController
{
	
	public function actionIndex($bookFlag)
	{
		
		$book = $this->getBookInfo($bookFlag);
		$chapters = $this->getBookTableOfContents($book->moduleId);

		$this->navText = '书籍: '.$book->bookName.': 章节管理';
		
		$this->render('index', array(
				'chapters'=>$chapters,
				'book'=>$book
		));
	}
	
	public function actionCreate($bookFlag)
	{
		$book = $this->getBookInfo($bookFlag);
		Chapter::setTableId($book->moduleId);
		$chapter = new Chapter;
		if (isset($_POST['Chapter'])) {
			$chapter->attributes = $_POST['Chapter'];
			if ($chapter->save()) {
				$this->clearChapterCache($bookFlag, $chapter->url);
				$this->clearChapterListCache($book->moduleId);
				$this->redirect(array('chapter/index', 'bookFlag'=> $bookFlag));
			}
		}
		$this->navText = '书籍: '.$book->bookName.': 新建章节';
		$this->render('create', array(
				'chapter'=>$chapter,
				'book'=>$book
		));
	}
	
	public function actionUpdate($bookFlag, $chapterFlag)
	{
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		
		Chapter::setTableId($book->moduleId);
		$chapter = Chapter::model()->findbyPk($chapter['chapterId']);
		if (isset($_POST['Chapter'])) {
			$chapter->attributes = $_POST['Chapter'];
			if ($chapter->save()) {
				$this->clearChapterCache($bookFlag, $chapter->url);
				$this->clearChapterListCache($book->moduleId);
				$this->redirect(array('chapter/index', 'bookFlag'=> $bookFlag));
			}
		}
		$this->navText = '书籍: '.$book->bookName.': 修改章节';
		$this->render('create', array(
				'chapter'=>$chapter,
				'book'=>$book
		));
	}
	
	public function clearChapterListCache($bookId) {
		Yii::app()->cache->delete($this->getCacheId('tableOfContents', $bookId));
	}

	public function clearChapterCache($bookFlag, $chapterFlag) {
		Yii::app()->cache->delete($this->getCacheId('chapter', $bookFlag.$chapterFlag));
	}

	public function clearChapterPageCache($bookFlag, $chapterFlag) {
		//REMOVE CACHE
		$cacheKey = COutputCache::CACHE_KEY_PREFIX.'book_default..book/default/chapter...' . $bookFlag.'-'.$chapterFlag.'.';
		Yii::app()->cache->delete($cacheKey);
	}
	
	/**
	 * 预览前台
	 * @param string $bookFlag
	 * @param string $chapterFlag
	 */
	public function actionPreview($bookFlag, $chapterFlag) {
		$this->clearChapterPageCache($bookFlag, $chapterFlag);
		$this->clearExpressNavCache($bookFlag, $chapterFlag);
		
		$this->redirect(array('/book/default/chapter', 'bookFlag'=>$bookFlag, 'chapterFlag'=>$chapterFlag));
	}

	/**
	 * 段落排序
	 */
	public function actionOrder()
	{
		$bookFlag = $_GET['bookFlag'];
		$book = $this->getBookInfo($bookFlag);
		if (isset($_POST['ChapterOrderForm'])) {
			$post = $_POST['ChapterOrderForm'];
			$orderModel = new SectionOrderForm();
			$orderModel->setAttributes($post, false);
			if ($orderModel->validate()) {
				$chapters = Yii::app()->db->createCommand()->from('{{module_'.$book->moduleId.'_chapters}}')
					->order('chapterSort DESC')->queryAll();
				$i = $orderModel->begin_num;
				foreach($chapters as $cha) {
					Yii::app()->db->createCommand()->update(
							'{{module_'.$book->moduleId.'_chapters}}',
							array('chapterSort'=>$i),
							'chapterId=:chapterId', array('chapterId'=>$cha['chapterId'])
					);
					$i=$i-$orderModel->setp_num;
				}
	
				$this->clearChapterListCache($book->moduleId);
				echo '{"success":"ok"}';
				exit;
			}
		}
	}
}